﻿using System;
using System.Collections.Generic;

using E.BLL;
using E.Model;
using E.Web.UI.Page;

using X.Common;

namespace E.Web.Teacher.internals.surveysetting
{
    public partial class CertificateEdit : TeacherBasePage
    {
        public IDictionary<int, string> optionList;
        public SurveySettingModel model = new SurveySettingModel();
        public IDictionary<int, string> certificateList = new Dictionary<int, string>();
        public string act;
        public int id;
        public string option_v;

        protected override void ShowPage()
        {
            id = XRequest.GetInt("id", -1);
            act = XRequest.GetString("act");

            //courseList = (new CourseBLL()).GetAllCourseList();

            SurveyBLL bll = new SurveyBLL();

            certificateList = bll.GetNotSetCertificateSurveyList();

            if (ispost)
            {
                string option = XRequest.GetString("option_v");
                string[] options = GetArray(option);

                if (CheckValue())
                {
                    model = new SurveySettingModel();
                    model.OptionIDStrList = options;

                    int rst = 0;
                    string href = "";

                    switch (act)
                    {
                        case "n":
                            int certificateid = XRequest.GetInt("CertificateSlt", -1);
                            model.CID = certificateid;

                            rst = bll.AddCertificateSurveySetting(model);

                            href = "/internals/surveysetting/CertificateEdit.aspx?act=n";

                            break;
                        case "u":
                            model.ID = id;

                            rst = bll.UpdateCertificateSurveySetting(model);

                            href = "/internals/surveysetting/CertificateEdit.aspx?act=u&id=" + id;
                            break;
                        default:
                            break;
                    }

                    if (rst > 0)
                    {
                        ExecuteJS("alert('操作成功'); window.location = '" + href + "';");
                    }
                    else
                    {
                        ExecuteJS("alert('操作失败'); histroy.back();");
                    }
                }
                else
                {
                    ExecuteJS("histroy.back();");
                }
            }
            else
            {
                optionList = bll.GetAllSurveyOptionList();
                model = bll.GetCertificateSurveySetting(id);

                IList<int> optionIDList = model.OptionIDList;
                option_v = Convert2String(optionIDList);
            }
        }

        private bool CheckValue()
        {
            bool rst = true;

            if (XRequest.GetString("act") == "n" && XRequest.GetInt("CertificateSlt", -1) == -1)
            {
                Alert("请选择认证或项目");
                rst = false;
            }
            if (XRequest.GetString("option_v").Equals(""))
            {
                Alert("请选择至少一个选项");
                rst = false;
            }
            return rst;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
